package com.xesj.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 
* @ClassName: DateUtil 
* @Description: 用来转换时间的工具类
* @author lee 
* @date 2015年5月21日 下午3:11:38 
*
 */
public class DateUtil {
    
    //date formats
	public static final String DATEFORMAT_STR0 = "yyyy-MM-dd HH:mm:ss.sss";
    public static final String DATEFORMAT_STR1 = "yyyy-MM-dd HH:mm:ss";
    public static final String DATEFORMAT_STR2 = "yyyy-MM-dd HH:mm";
    public static final String DATEFORMAT_STR7 = "yyyy-MM-dd HH";
    public static final String DATEFORMAT_STR3 = "yyyy-MM-dd";
    public static final String DATEFORMAT_STR4 = "yyyyMMdd";
    public static final String DATEFORMAT_STR5 = "yyMMdd";
    public static final String DATEFORMAT_STR6 = "yyyyMM";
    /** 
    * 获取  当前年、半年、季度、月、日、小时 开始结束时间 
    */ 
    private static SimpleDateFormat shortSdf;
    private static SimpleDateFormat longHourSdf; 
    private static SimpleDateFormat longSdf; 
    
    static{ 
        shortSdf = new SimpleDateFormat(DATEFORMAT_STR3); 
        longHourSdf = new SimpleDateFormat(DATEFORMAT_STR7); 
        longSdf = new SimpleDateFormat(DATEFORMAT_STR1); 
    }
	/**
	 * 时间转换方法
	 * @param date 传入一个String的时间
	 * @return 获得年月日的时间(yyyy-MM-dd)
	 */
	public static String formatDate(Date date){		
		//保存改后的时间
		String ymd=" ";
		if(date==null||"".equals(date)||"1900-01-01 00:00:00.0".equals(date)){
			return ymd;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		ymd=dateFormat.format(date);
		return ymd;
	}
	
	/**
	 * 
	 * 根据字符串格式化为时间
	 * @param format
	 * @return
	 */
	public static String getDateFormat(String strDate, String formatType){
		String ymd=" ";
		if(strDate==null||"".equals(strDate)){
		SimpleDateFormat sdf=new SimpleDateFormat(formatType);
		try {
			Date dt = sdf.parse(strDate);
			return sdf.format(dt);
		} catch (ParseException e) {
			e.printStackTrace();
			return ymd;
		}
		}else{
			return ymd;
		}
	}
	
	
	/**
	 * 
	* @Title: formatDate
	* @Description: Date类型转换成Sting
	* @param @param date   Date类型的时间
	* @param @param formatType 转换时间的类型
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
    public static String formatDate(Date date, String formatType){     
        //保存改后的时间

        String ymd=" ";
        if(date==null||"".equals(date)||"1900-01-01 00:00:00.0".equals(date)){
            return ymd;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatType);
        ymd=dateFormat.format(date);
        return ymd;
    }
    
    /**
     * 
    * @Title: formatDate
    * @Description: Sting类型转换成Date
    * @param @param str_date   String类型的时间
    * @param @param formatType 转换时间的类型
    * @param @return    设定文件
    * @return Date    返回类型
    * @throws
     */
    public static Date formatDate(String str_date, String formatType){     
        if(str_date==null||"".equals(str_date)||"1900-01-01 00:00:00".equals(str_date)){
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        try {
            date = formatter.parse(str_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    
    
    
    
	/**
	 * 
	 * @return 当前时间年
	 */
	public static String getYear() {
		return getCurrentDateTime("yyyy");
	}

	/**
	 * 获得给定时间的月分的会后一天
	 * @param calendar
	 * @return 天数
	 */
	public int monthLastDay(GregorianCalendar calendar) {
		
		int lastDay = 0;
		GregorianCalendar gregorian = calendar;		
		int month = gregorian.get(Calendar.MONTH);		
		do {
			lastDay = gregorian.get(Calendar.DAY_OF_MONTH);
			gregorian.add(Calendar.DAY_OF_MONTH, 1);
		} while (gregorian.get(Calendar.MONTH) == month);	
		return lastDay;
	}
	
	/**
	 * 返回这个月的最大Date
	 * @return YYYY-MM-dd
	 */
	public String getMonthLastDaDate(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.YEAR));
		datetime.append("-");
		if(calendar.get(Calendar.MONTH)<9){		
			datetime.append("0");
		}
		datetime.append((calendar.get(Calendar.MONTH)+1));
		datetime.append("-");
		datetime.append(monthLastDay(calendar));
		return datetime.toString();
	}
	/**
	 * 返回这个月的最小Date
	 * @return YYYY-MM-dd
	 */
	public String getMonthLeastDaDate(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.YEAR));
		datetime.append("-");
		if(calendar.get(Calendar.MONTH)<9){		
			datetime.append("0");
		}
		datetime.append((calendar.get(Calendar.MONTH)+1));
		datetime.append("-");
		datetime.append("01");
		return datetime.toString();
	}
	
	/**
	 * 获得当前日期YYYY-MM-dd
	 * @return YYYY-MM-dd
	 */
	public String getFormatDate(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.YEAR));
		datetime.append("-");
		if(calendar.get(Calendar.MONTH)<9){		
			datetime.append("0");
		}
		datetime.append((calendar.get(Calendar.MONTH)+1));
		datetime.append("-");
		if(calendar.get(Calendar.DAY_OF_MONTH)<10){
			datetime.append("0");
		}
		datetime.append(calendar.get(Calendar.DAY_OF_MONTH));
		return datetime.toString();
	}
	
	/**
	 * 获得 XXXX年XX月XX日XX分XX秒 格式的的时间
	 * @return
	 */
	public String getFormatTime(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.YEAR));
		datetime.append("年");
		datetime.append((calendar.get(Calendar.MONTH)+1));
		datetime.append("月");	
		datetime.append(calendar.get(Calendar.DAY_OF_MONTH));
		datetime.append("日");
		datetime.append(calendar.get(Calendar.MINUTE));
		datetime.append("分");
		datetime.append(calendar.get(Calendar.SECOND));
		datetime.append("秒");
		return datetime.toString();
	}
	
	/**
	 * 获得 当前日期YYYYMMdd
	 * @return
	 */
	public static  String getDate(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.YEAR));
		if(calendar.get(Calendar.MONTH)<9){		
			datetime.append("0");
		}
		datetime.append((calendar.get(Calendar.MONTH)+1));
		if(calendar.get(Calendar.DAY_OF_MONTH)<10){
			datetime.append("0");
		}
		datetime.append(calendar.get(Calendar.DAY_OF_MONTH));
		return datetime.toString();
	}
	
	
	/**
	 *  获得当前时间 HH:MM
	 * @return
	 */
	public String getTime(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.HOUR_OF_DAY));
		datetime.append(":");
		datetime.append(calendar.get(Calendar.MINUTE));
		return datetime.toString();
	}
	
	/**
	 *  获得当前时间 HHmmss
	 * @return
	 */
	public static String getFullTimeNotDate(){
		return getCurrentDateTime("HHmmss");
	}
	/**
	 *  获得当前时间 HHmmss
	 * @return
	 */
	public static String getFullTimeByTwoPonit(){
		return getCurrentDateTime("HH:mm:ss");
	}
	/**
	 *  获得当前时间 yyyyMMdd
	 * @return
	 */
	public static String getFullDate(){
		return getCurrentDateTime("yyyyMMdd");
	}
	/**
	 * 获得当前时间 yyyy-MM-dd
	 * @return
	 */
	public static String getFullDate_(){
		return getCurrentDateTime("yyyy-MM-dd");
	}
	/**
	 * 根据格式返回当前日期时间
	 * @param format
	 * @return
	 */
	public static String getCurrentDateTime(String format){
		SimpleDateFormat df=new SimpleDateFormat(format);
		return df.format(new Date());
	}
	/**
	 * 返回星期
	 * @return
	 */
	public static String getWeekay() {
		String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DAY_OF_WEEK);
		String week_day = dayNames[day - 1];
		return week_day;
	}
	
	public static String getDateDay() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
		return df.format(new Date());
	}
	
	public static String dateFormat(Date date,String format){
		if (StrValidate.isNull(date)){
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}
	
	/**
	 * 返回YYYY-MM-dd
	 * @param date
	 * @return
	 */
	public static String getStrDate(String date){
		String y=date.substring(0,4);
		String m=date.substring(4,6);
		String d=date.substring(6,8);
		StringBuffer newDate=new StringBuffer();
		newDate.append(y).append("-").append(m).append("-").append(d);
		return newDate.toString();
	}
	/**
	 * 返回YYYY-MM-dd
	 * @param date
	 * @return
	 */
	public static String getStrDateTime(String date){
		String y=date.substring(0,4);
		String m=date.substring(4,6);
		String d=date.substring(6,8);
		StringBuffer newDate=new StringBuffer();
		newDate.append(y).append("-").append(m).append("-").append(d).append(" ");
		String h=date.substring(8,10);
		String mm=date.substring(10,12);
		String s=date.substring(12,14);
		newDate.append(h).append(":").append(mm).append(":").append(s);
		return newDate.toString();
	}
	/**
	 * 返回HH:mm:ss
	 * @param date
	 * @return
	 */
	public static String getStrtime(String date){
		String y=date.substring(0,2);
		String m=date.substring(2,4);
		String d=date.substring(4,6);
		StringBuffer newDate=new StringBuffer();
		newDate.append(y).append(":").append(m).append(":").append(d);
		return newDate.toString();
	}
	/**
	 * 判断是否为现在日期
	 * @param date
	 * @return
	 */
	public static boolean isCurrent(String date){
		boolean flag = true;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
			Date dt = df.parse(date);
			Calendar cal = Calendar.getInstance();
			cal.setTime(dt);
			Calendar currentDate = Calendar.getInstance();
			if(currentDate.get(Calendar.YEAR)!=cal.get(Calendar.YEAR)
			  ||currentDate.get(Calendar.MONTH)!=cal.get(Calendar.MONTH)
			  ||currentDate.get(Calendar.DATE)!=cal.get(Calendar.DATE)){
				flag = false;
			}
		} catch (ParseException e) {
		}
		return flag;
	}
	
	public static Calendar stringToCalendar(String date){
		Calendar cal = Calendar.getInstance();
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
			Date dt = df.parse(date);
			cal.setTime(dt);
		} catch (ParseException e) {
		}
		return cal;
	}
	
	public static String calendarToString(Calendar cal){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 获取完整的时间日期
	 * @return
	 */
	public static String getFullTime() {
		return getCurrentDateTime("yyyy-MM-dd HH:mm:ss");
	}
	/**
	 * 获取完整的时间日期
	 * @return
	 */
	public static String getYM() {
		return getCurrentDateTime("yyyy-MM");
	}
	
	/**
	 * 获取完整的时间日期

	 * @return
	 */
	public static String getYearM() {
		return getCurrentDateTime("yyyyMM");
	}
	
	/**
	 * 获取完整的时间日期时间yyyyMMddHHmmss
	 * @return
	 */
	public static String getFullString(){
		return getCurrentDateTime("yyyyMMddHHmmss");
	}
	
	/**
	 * 
	 * 根据字符串格式化为时间
	 * @param format
	 * @return
	 */
	public static Date getDateFormat(String format){
		if(format==null||"".equals(format)){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(format);
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}
		}else{
			return new Date();
		}
	}

	// 测试方法
	public static void main(String[] args) {
		List<String> dayWeekList=new ArrayList<String>();
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DAY_OF_WEEK);
		cal.add(Calendar.DAY_OF_YEAR, -(day-1));
		dayWeekList.add(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
		for (int i = 0; i < 6; i++) {
			cal.add(Calendar.DAY_OF_YEAR, 1);
			dayWeekList.add(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
		}
		System.out.println(dayWeekList);
	}
	
	public static List getAllDate(String start, String end) throws Exception {
		List list = new ArrayList();
		Date dateTemp = (Date) new SimpleDateFormat("yyyy-MM-dd").parse(start);
		Date dateTemp2 = (Date) new SimpleDateFormat("yyyy-MM-dd").parse(end);
		Calendar calendarTemp = Calendar.getInstance();
		calendarTemp.setTime(dateTemp);
		while (calendarTemp.getTime().getTime()<= dateTemp2.getTime()) {
		    list.add(new SimpleDateFormat("yyyy-MM-dd").format(calendarTemp.getTime()));
		    calendarTemp.add(Calendar.DAY_OF_YEAR, 1);
		}
		return list;
    }
	
	/** 
     * 获得本小时的开始时间，即2012-01-01 23:59:59 
     * 
     * @return 
     */ 
    public static Date getCurrentHourStartTime() { 
        Date now = new Date(); 
        try { 
            now = longHourSdf.parse(longHourSdf.format(now)); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    } 

    /** 
     * 获得本小时的结束时间，即2012-01-01 23:59:59 
     * 
     * @return 
     */ 
    public static Date getCurrentHourEndTime() { 
        Date now = new Date(); 
        try { 
            now = longSdf.parse(longHourSdf.format(now) + ":59:59"); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    }
	
	/** 
     * 获得本天的开始时间，即2012-01-01 00:00:00 
     * 
     * @return 
     */ 
    public static Date getCurrentDayStartTime() { 
        Date now = new Date(); 
        try { 
            now = shortSdf.parse(shortSdf.format(now)); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    } 

    /** 
     * 获得本天的结束时间，即2012-01-01 23:59:59 
     * 
     * @return 
     */ 
    public static Date getCurrentDayEndTime() { 
        Date now = new Date(); 
        try { 
            now = longSdf.parse(shortSdf.format(now) + " 23:59:59"); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    } 
	
	 /** 
     * 获得本周的第一天，周一 
     * 
     * @return 
     */ 
    public static Date getCurrentWeekDayStartTime() { 
        Calendar c = Calendar.getInstance(); 
        try { 
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2; 
            c.add(Calendar.DATE, -weekday); 
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00")); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return c.getTime(); 
    } 

    /** 
     * 获得本周的最后一天，周日 
     * 
     * @return 
     */ 
    public static Date getCurrentWeekDayEndTime() { 
        Calendar c = Calendar.getInstance(); 
        try { 
            int weekday = c.get(Calendar.DAY_OF_WEEK); 
            c.add(Calendar.DATE, 8 - weekday); 
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59")); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return c.getTime(); 
    } 

    /** 
     * 获得本月的开始时间，即2012-01-01 00:00:00 
     * 
     * @return 
     */ 
    public static Date getCurrentMonthStartTime() { 
        Calendar c = Calendar.getInstance(); 
        Date now = null; 
        try { 
            c.set(Calendar.DATE, 1); 
            now = shortSdf.parse(shortSdf.format(c.getTime())); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    } 

    /** 
     * 当前月的结束时间，即2012-01-31 23:59:59 
     * 
     * @return 
     */ 
    public static Date getCurrentMonthEndTime() { 
        Calendar c = Calendar.getInstance(); 
        Date now = null; 
        try { 
            c.set(Calendar.DATE, 1); 
            c.add(Calendar.MONTH, 1); 
            c.add(Calendar.DATE, -1); 
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    } 

    /** 
     * 当前年的开始时间，即2012-01-01 00:00:00 
     * 
     * @return 
     */ 
    public static Date getCurrentYearStartTime() { 
        Calendar c = Calendar.getInstance(); 
        Date now = null; 
        try { 
            c.set(Calendar.MONTH, 0); 
            c.set(Calendar.DATE, 1); 
            now = shortSdf.parse(shortSdf.format(c.getTime())); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    } 

    /** 
     * 当前年的结束时间，即2012-12-31 23:59:59 
     * 
     * @return 
     */ 
    public static Date getCurrentYearEndTime() { 
        Calendar c = Calendar.getInstance(); 
        Date now = null; 
        try { 
            c.set(Calendar.MONTH, 11); 
            c.set(Calendar.DATE, 31); 
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    } 

    /** 
     * 当前季度的开始时间，即2012-01-1 00:00:00 
     * 
     * @return 
     */ 
    public static Date getCurrentQuarterStartTime() { 
        Calendar c = Calendar.getInstance(); 
        int currentMonth = c.get(Calendar.MONTH) + 1; 
        Date now = null; 
        try { 
            if (currentMonth >= 1 && currentMonth <= 3) 
                c.set(Calendar.MONTH, 0); 
            else if (currentMonth >= 4 && currentMonth <= 6) 
                c.set(Calendar.MONTH, 3); 
            else if (currentMonth >= 7 && currentMonth <= 9) 
                c.set(Calendar.MONTH, 4); 
            else if (currentMonth >= 10 && currentMonth <= 12) 
                c.set(Calendar.MONTH, 9); 
            c.set(Calendar.DATE, 1); 
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    } 

    /** 
     * 当前季度的结束时间，即2012-03-31 23:59:59 
     * 
     * @return 
     */ 
    public static Date getCurrentQuarterEndTime() { 
        Calendar c = Calendar.getInstance(); 
        int currentMonth = c.get(Calendar.MONTH) + 1; 
        Date now = null; 
        try { 
            if (currentMonth >= 1 && currentMonth <= 3) { 
                c.set(Calendar.MONTH, 2); 
                c.set(Calendar.DATE, 31); 
            } else if (currentMonth >= 4 && currentMonth <= 6) { 
                c.set(Calendar.MONTH, 5); 
                c.set(Calendar.DATE, 30); 
            } else if (currentMonth >= 7 && currentMonth <= 9) { 
                c.set(Calendar.MONTH,4); 
                c.set(Calendar.DATE, 30); 
            } else if (currentMonth >= 10 && currentMonth <= 12) { 
                c.set(Calendar.MONTH, 11); 
                c.set(Calendar.DATE, 31); 
            } 
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    } 
    /** 
     * 获取前/后半年的开始时间 
     * @return 
     */ 
    public static Date getHalfYearStartTime(){ 
        Calendar c = Calendar.getInstance(); 
        int currentMonth = c.get(Calendar.MONTH) + 1; 
        Date now = null; 
        try { 
            if (currentMonth >= 1 && currentMonth <= 6){ 
                c.set(Calendar.MONTH, 0); 
            }else if (currentMonth >= 7 && currentMonth <= 12){ 
                c.set(Calendar.MONTH, 6); 
            } 
            c.set(Calendar.DATE, 1); 
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
        
    } 
    /** 
     * 获取前/后半年的结束时间 
     * @return 
     */ 
    public static Date getHalfYearEndTime(){ 
        Calendar c = Calendar.getInstance(); 
        int currentMonth = c.get(Calendar.MONTH) + 1; 
        Date now = null; 
        try { 
            if (currentMonth >= 1 && currentMonth <= 6){ 
                c.set(Calendar.MONTH, 5); 
                c.set(Calendar.DATE, 30); 
            }else if (currentMonth >= 7 && currentMonth <= 12){ 
                c.set(Calendar.MONTH, 11); 
                c.set(Calendar.DATE, 31); 
            } 
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return now; 
    }  
}
